package com.coffee.core.util;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

@Slf4j
public class ZhongtailClient {
    private static final String APP_ID = "A56Pshsi";
    public static final String ZHONGTAI_APP_KEY = "1499317825548980224";
    public static final String ZHONGTAI_APP_SECRET = "e699ee9e43ce0a63a2e6edf973d6d9ce";
    public static final String ZHONGTAI_URL = "https://zt.51jujibao.com";


    public static ToolIpSegmentResult getIpSeg(String ip) {
        ToolIpSegOpenApiRequest req = new ToolIpSegOpenApiRequest();
        req.setIp(ip);
        req.setBusinessKey(APP_ID);
        req.setAppKey(ZHONGTAI_APP_KEY);
        String sign = getZhongtaiSign(req, ZHONGTAI_APP_SECRET);
        req.setSign(sign);
        String result = HttpRequest.post(ZHONGTAI_URL + "/api/tool/ipSeg").body(JSONObject.toJSONString(req)).execute().body();
        JSONObject json = JSONObject.parseObject(result);
        String code = json.getString("code");
        String message = json.getString("message");
        if (!"00".equals(code)) {
            log.error("===getIpSeg ip=" + ip + " error: errCode=" + code + ",errMsg:" + message);
            return null;
        }
        return json.getObject("result", ToolIpSegmentResult.class);
    }

    public static String getZhongtaiSign(Object req, String appSecret) {
        Map<String, String> param = objectToMap(req);
        TreeMap<String, String> treeMap = new TreeMap<String, String>(param);

        String plainStr = "";
        for (Map.Entry<String, String> entry : treeMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (!"sign".equals(key) && StringUtils.isNotEmpty(value)) {
                plainStr += key + "=" + value + "&";
            }
        }
        plainStr += "" + appSecret;

        return DigestUtil.md5Hex(plainStr);
    }

    public static ToolMobileSegmentResult getMobileSeg(String mobile) {
        ToolMobileSegOpenApiRequest req=new ToolMobileSegOpenApiRequest();
        req.setMobile(mobile);
        req.setBusinessKey(APP_ID);
        req.setAppKey(ZHONGTAI_APP_KEY);
        String sign = getZhongtaiSign(req, ZHONGTAI_APP_SECRET);
        req.setSign(sign);
        String result = HttpRequest.post(ZHONGTAI_URL + "/api/tool/mobileSeg").body(JSONObject.toJSONString(req)).execute().body();
        JSONObject json = JSONObject.parseObject(result);
        String code = json.getString("code");
        String message = json.getString("message");
        if (!"00".equals(code)) {
            log.error("===getMobileSeg mobile="+mobile+" error: errCode=" + code + ",errMsg:" + message);
            return null;
        }
        return json.getObject("result", ToolMobileSegmentResult.class);
    }

    public static Map<String, String> objectToMap(Object obj) {
        Map<String, String> map = new HashMap<>();// 定义返回值Map
        if (obj == null) {
            return map;
        }
        Class<?> clazz = obj.getClass();// 获取类的字节码
        Field[] fields = clazz.getDeclaredFields();// 获取类里面的所有的成员变量
        for (Field field : fields) {// 对成员变量数组进行便利
            field.setAccessible(true); // 临时取消成员变量权限修饰符
            String fieldName = field.getName();// 获取成员变量名
            Object fieldValue;// 定义成员变量值接收对象
            try {
                fieldValue = field.get(obj);// 接收成员变量
            } catch (Exception e) {
                fieldValue = new Object();// 接受失败将接受对象重置
            }
            map.put(fieldName, fieldValue.toString());
        }
        return map;
    }

    public static void main(String[] args) {
        System.out.println();
    }

}
